import {
	get,
	post
} from '../../utils/request.js'
Page({
	data: {
		active: 3,
		classifyId: '',
		navList: [],
		currentNav: [],
		goodsList: [],
		topIconIsShow: false,
	},
	onLoad: function(options) {
		// console.log(options)
		const id = options.id
		this.setData({
			classifyId: id
		})
		this.getAllData()
		// console.log(typeof(this.data.active))
	},
	async getAllData() {
		// console.log(this.data.classifyId)
		const data = await get('/category/categpryNav', {
			id: this.data.classifyId
		})
		// console.log(data)
		this.setData({
			navList: data.navData,
			currentNav: data.currentNav
		})
		for (let i = 0; i < this.data.navList.length; i++) { // 处理active类名
			const id = this.data.navList[i].id
			if (id == this.data.currentNav.id) {
				this.setData({
					active: id
				})
			}
		}
		const listData = await get('/goods/goodsList', {
			categoryId: this.data.classifyId
		})
		// console.log(listData.data)
		this.setData({
			goodsList: listData.data
		})
	},
	async changeTab(e) {
		// console.log(e.detail.name)
		const listData = await get('/goods/goodsList', {
			categoryId: e.detail.name
		})
		// console.log(listData)
		this.setData({
			goodsList: listData.data,
			currentNav: listData.currentNav,
			active: e.detail.index
		})
	},
	goToDetail (e) {
		let id = e.currentTarget.dataset.id
		// console.log(id)
		wx.navigateTo({
			url: `/pages/goodsdetail/goodsdetail?id=${id}`
		})
	},
	onPageScroll(e) { // page页面的监听滚动的事件
		if (e.scrollTop > 100) { // 如果滚动大于100 就显示top组件
			this.setData({
				topIconIsShow: true
			});
		} else {
			this.setData({
				topIconIsShow: false
			});
		}
	},
})
